package com.cindata

import com.opencsv.CSVReader

class CSVImpoter {
    public columns = []
    public startRow = 1
    /************执行读取csv方法************/
    def read(InputStream inputStream,String charsetName)
    {
        if(!charsetName){
            charsetName = "UTF-8"
        }
        def list = []
        InputStreamReader fileReader = new InputStreamReader(inputStream, charsetName)
        CSVReader reader = new CSVReader(fileReader)
        String[] nextLine
        int i = 1
        while ((nextLine = reader.readNext()) != null) {
            if (i>=startRow) {
                System.out.println("string"+nextLine)
                def map = [:]
                for(int j=0;j<columns.size();j++){
                    map.put(columns[j]["name"],readColumn(columns[j],nextLine[j]))
                }
                list.push(map)
            }
            i++
        }
        return list
    }

    def readColumn(Map headColumn,String data){
        if(headColumn.containsKey("type")){
            if(headColumn["type"] == "Date" && data!=""){
                return Date.parse("yyyy/MM/dd",data)
            }
        }
        else{
            return data
        }
    }
}
